package gui;

// The HistoryGraph, used for getting data from the database
import graphs.HistoryGraph;

// The listeners, listen for user input
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

// Swing specific imports
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSeparator;
import javax.swing.JTextField;

import org.dyno.visual.swing.layouts.Constraints;
import org.dyno.visual.swing.layouts.GroupLayout;
import org.dyno.visual.swing.layouts.Leading;

/**
 * The Geschiedenis class creates the Geschiedenis tab
 * 
 * @version 31-03-2009
 */
public class Geschiedenis implements ActionListener {

	// The main panel which contains all the components
	private JPanel panel;
	// A container for holding the date fields and confirm button
	private JPanel container;

	// The confirmation button
	private JButton button_Dag;

	// The seperator between the title and the components
	private JSeparator sep_Geschiedenis;

	// The labels for the date
	private JLabel label_Jaar;
	private JLabel label_Maand;
	private JLabel label_Dag;

	// The label which shows the status of the generator
	private JLabel label_Message;

	// The textfields for the entering of the date
	private JTextField txt_Jaar;
	private JTextField txt_Maand;
	private JTextField txt_Dag;

	/**
	 * Constructor
	 */
	public Geschiedenis() {

	}

	/**
	 * Creates the main panel
	 * 
	 * @return The main Geschiedenis panel
	 */
	public JPanel getPanel() {
		if (panel == null) {
			panel = new JPanel();
			panel.setLayout(new GroupLayout());
			panel.add(getContainer(), new Constraints(new Leading(10, 12, 12), new Leading(45, 12, 12)));
			panel.add(getLabel_Message(), new Constraints(new Leading(10, 12, 12), new Leading(10, 12, 12)));
			panel.add(getSep_Geschiedenis(), new Constraints(new Leading(6, 151, 12, 12), new Leading(31, 10, 12, 12)));
		}
		return panel;
	}

	/**
	 * Creates the user input panel
	 * 
	 * @return The user input panel
	 */
	public JPanel getContainer() {
		if (container == null) {
			container = new JPanel();
			container.setLayout(new GroupLayout());
			container.add(getLabel_Dag(), new Constraints(new Leading(0, 12, 12), new Leading(0, 12, 12)));
			container.add(getLabel_Maand(), new Constraints(new Leading(40, 12, 12), new Leading(0, 12, 12)));
			container.add(getLabel_Jaar(), new Constraints(new Leading(80, 12, 12), new Leading(0, 12, 12)));
			container.add(getButton_Dag(), new Constraints(new Leading(120, 12, 12), new Leading(7, 12, 12)));
			container.add(getTxT_Dag(), new Constraints(new Leading(0, 12, 12), new Leading(14, 12, 12)));
			container.add(getTxT_Maand(), new Constraints(new Leading(40, 12, 12), new Leading(14, 12, 12)));
			container.add(getTxT_Jaar(), new Constraints(new Leading(80, 12, 12), new Leading(14, 12, 12)));
		}
		return container;
	}

	/**
	 * Creates the confirmation button
	 * 
	 * @return The confirmation button
	 */
	private JButton getButton_Dag() {
		if (button_Dag == null) {
			button_Dag = new JButton();
			button_Dag.setText("Ok");
			button_Dag.addActionListener(this);
		}
		return button_Dag;
	}

	/**
	 * Creates the day label
	 * 
	 * @return The day label
	 */
	private JLabel getLabel_Dag() {
		if (label_Dag == null) {
			label_Dag = new JLabel();
			label_Dag.setText("Dag");
		}
		return label_Dag;
	}

	/**
	 * Creates the month label
	 * 
	 * @return The month label
	 */
	private JLabel getLabel_Maand() {
		if (label_Maand == null) {
			label_Maand = new JLabel();
			label_Maand.setText("Mnd");
		}
		return label_Maand;
	}

	/**
	 * Creates the year label
	 * 
	 * @return The year label
	 */
	private JLabel getLabel_Jaar() {
		if (label_Jaar == null) {
			label_Jaar = new JLabel();
			label_Jaar.setText("Jaar");
		}
		return label_Jaar;
	}

	/**
	 * Creates the message label
	 * 
	 * @return The message label
	 */
	private JLabel getLabel_Message() {
		if (label_Message == null) {
			label_Message = new JLabel();
			label_Message.setText("Selecteer datum");
		}
		return label_Message;
	}

	/**
	 * Creates the seperator between the title and the input components
	 * 
	 * @return The seperator
	 */
	private JSeparator getSep_Geschiedenis() {
		if (sep_Geschiedenis == null) {
			sep_Geschiedenis = new JSeparator();
		}
		return sep_Geschiedenis;
	}

	/**
	 * Creates the textfield for the year input
	 * 
	 * @return The year textfield
	 */
	private JTextField getTxT_Jaar() {
		if (txt_Jaar == null) {
			txt_Jaar = new JTextField(3);
		}
		return txt_Jaar;
	}

	/**
	 * Creates the textfield for the month input
	 * 
	 * @return The month textfield
	 */
	private JTextField getTxT_Maand() {
		if (txt_Maand == null) {
			txt_Maand = new JTextField(2);
		}
		return txt_Maand;
	}

	/**
	 * Creates the textfield for the day input
	 * 
	 * @return The day textfield
	 */
	private JTextField getTxT_Dag() {
		if (txt_Dag == null) {
			txt_Dag = new JTextField(2);
		}
		return txt_Dag;
	}

	/**
	 * Listenes for user input on the confirmation button
	 */
	public void actionPerformed(ActionEvent e) {
		// Get the day
		String dag = this.txt_Dag.getText();
		// Get the month
		String maand = this.txt_Maand.getText();
		// Get the year
		String jaar = this.txt_Jaar.getText();

		// Check if all fields are filled out correctly
		if (dag.equals("")) {
			this.label_Message.setText("Kies dag!");
		} else if (maand.equals("")) {
			this.label_Message.setText("Kies maand!");
		} else if (jaar.equals("")) {
			this.label_Message.setText("Kies jaar!");
		} else {
			// If the day or month values are one digit, add an extra '0' to the
			// beginning
			if (dag.length() == 1) {
				dag = "0" + dag;
			}
			if (maand.length() == 1) {
				maand = "0" + maand;
			}

			// Create the graphic
			this.label_Message.setText("Grafiek wordt gemaakt (" + dag + "/" + maand + "/" + jaar + ")");
			HistoryGraph hg = new HistoryGraph(dag, maand, jaar);

			// Print the graphic in a new window
			hg.printChart();
		}
	}
}